home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / mgr.zoo / mgrdif1.zoo / src / new_window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-01  |  7.8 KB  |  366 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: new_window.c,v 1.1 89/03/17 08:21:17 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/new_window.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/new_window.c,v $$Revision: 1.1 $";
  12.  
  13. /* Create a new window */
  14.  
  15. #include "bitmap.h"
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include "defs.h"
  19. #include "window.h"
  20. #include "font.h"
  21. #include "menu.h"
  22.  
  23. #ifdef atarist
  24. #define FNDELAY O_NDELAY
  25. #endif
  26.  
  27. /* sweep out a new window */
  28.  
  29. int new_window()
  30.    {
  31.    register WINDOW *win;
  32.    int dx=16,dy=16;
  33.    char *malloc();
  34.    WINDOW * insert_win();
  35.  
  36.    if (next_window >= MAXWIN)
  37.        return(-1);
  38.    SETMOUSEICON(&mouse_box);
  39.    move_mouse(screen,mouse,&mousex,&mousey,0);
  40.    SETMOUSEICON(&mouse_arrow);
  41.    get_rect(screen,mouse,mousex,mousey,&dx,&dy,0);
  42.    do_button(0);
  43.  
  44.    return( create_window( mousex, mousey, dx, dy, -1, 0 ) );
  45.    }
  46.  
  47. /* insert a new window into the window list */
  48.  
  49. WINDOW *
  50. insert_win(win)
  51. WINDOW *win;
  52.    {
  53.    char *malloc();
  54.  
  55.    if (win == (WINDOW *) 0 &&
  56.            (win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  57.       if( debug )
  58.      fprintf(stderr,"Can't malloc window space\n");
  59.       return(win);
  60.       }
  61.  
  62.    if (active) {
  63.       W(prev) = ACTIVE(prev);
  64.       ACTIVE(prev) = win;
  65.       W(next) = active;
  66.       }
  67.    else {
  68.       W(prev) = win;
  69.       W(next) = (WINDOW *) 0;
  70.       }
  71.    return(win);
  72.    }
  73.  
  74. /* create a new window given coords */
  75.  
  76. int
  77. create_window(x,y,dx,dy,font_num,argv)
  78. int x,y,dx,dy;
  79. int font_num;
  80. char **argv;
  81.    {
  82.    register WINDOW * win;
  83.    WINDOW * insert_win();
  84.  
  85.    if (next_window >= MAXWIN)
  86.        return(-1);
  87.    if (check_window(x,y,dx,dy,font_num) == 0)
  88.       return(-1);
  89.  
  90.    /* alloc window space */
  91.  
  92.    if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  93.       fprintf(stderr,"Can't malloc window space\n");
  94.       return(-1);
  95.       }
  96.       
  97.    if ((W(pid) = get_command(argv,&W(from_fd))) < 0) {
  98.       free(win);
  99.       fprintf(stderr,"Can't get a pty\n");
  100.       return(-1);
  101.       }
  102.    W(to_fd) = W(from_fd);
  103.    W(setid) = next_windowset_id();
  104.  
  105.    active = insert_win(win);
  106.  
  107.    make_window(screen,x,y,dx,dy,font_num,"");
  108.    return(0);
  109.    }
  110.  
  111. /* create a new window given coords , with only 1/2 a ptty */
  112.  
  113. char *
  114. half_window(x,y,dx,dy,font_num)
  115. int x,y,dx,dy;
  116. int font_num;
  117.    {
  118.    register WINDOW * win;
  119.    WINDOW * insert_win();
  120.    char *half_open();
  121.    char *tty;
  122.  
  123.    if (next_window >= MAXWIN)
  124.        return(NULL);
  125.    if (check_window(x,y,dx,dy,font_num) == 0)
  126.       return(NULL);
  127.  
  128.    /* alloc window space */
  129.  
  130.    if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  131.       fprintf(stderr,"Can't malloc window space\n");
  132.       return(NULL);
  133.       }
  134.       
  135.    if ((tty = half_open(&W(from_fd))) == NULL) {
  136.       free(win);
  137.       fprintf(stderr,"Can't get a pty\n");
  138.       return(NULL);
  139.       }
  140.  
  141.    W(setid) = next_windowset_id();
  142.    active = insert_win(win);
  143.  
  144.    W(to_fd) = W(from_fd);
  145.    make_window(screen,x,y,dx,dy,font_num,"");
  146.    W(pid) = 1;
  147.    W(flags) |= W_NOKILL;
  148.  
  149.    return(tty);
  150.    }
  151.  
  152. /* check window size */
  153.  
  154. int
  155. check_window(x,y,dx,dy,fnt)
  156. int x, y, dx, dy;
  157. int fnt;
  158.    {
  159.    struct font *curr_font, *Get_font();
  160.  
  161.    if (dx<0)
  162.       x += dx, dx = -dx;
  163.    if (dy<0)
  164.       y += dy, dy = -dy;
  165.    
  166.    if (x >= BIT_WIDE(screen) || y >= BIT_HIGH(screen))
  167.        return(0);
  168.  
  169.    if (x + dx >= BIT_WIDE(screen))
  170.       dx = BIT_WIDE(screen)-x;
  171.  
  172.    if (y + dy >= BIT_HIGH(screen))
  173.       dy = BIT_HIGH(screen)-y;
  174.  
  175.    curr_font = Get_font(fnt);
  176.  
  177. #ifdef DEBUG
  178.    dprintf(n)(stderr,"starting: (%d,%d)  %d x %d\r\n",x,y,dx,dy);
  179. #endif
  180.  
  181.    if (dx < SUM_BDR + curr_font->head.wide*MIN_X +1 ||
  182.               dy < SUM_BDR + curr_font->head.high*MIN_Y +1)
  183.       return(0);
  184.    else
  185.       return(1);
  186.    }
  187.  
  188. /* draw the window on the screen */
  189.  
  190. make_window(screen,x,y,dx,dy,fnt,start)
  191. BITMAP *screen;
  192. int x, y, dx, dy;
  193. int fnt;
  194. char *start;
  195.    {
  196.    register WINDOW *win = active;
  197.    register int i;
  198.    struct font *curr_font, *Get_font();
  199.    char *last_tty();
  200.  
  201.    if (dx<0)
  202.       x += dx, dx = -dx;
  203.    if (dy<0)
  204.       y += dy, dy = -dy;
  205.    
  206.    if (x < 0) x = 0;
  207.  
  208.    if (x + dx >= BIT_WIDE(screen))
  209.       dx = BIT_WIDE(screen)-x;
  210.  
  211.    if (y + dy >= BIT_HIGH(screen))
  212.       dy = BIT_HIGH(screen)-y;
  213.  
  214.    curr_font = Get_font(fnt);
  215.    if (curr_font == font) {
  216. #ifdef DEBUG
  217.       dprintf(n)(stderr,"Can't find font %d, using default\r\n", fnt);
  218. #endif
  219.       }
  220.  
  221. #ifdef DEBUG
  222.    dprintf(n)(stderr,"starting window: (%d,%d)  %d x %d font (%d,%d)\r\n",
  223.              x,y,dx,dy,curr_font->head.wide, curr_font->head.high);
  224.    dprintf(n)(stderr,"min size: %d x %d\r\n",
  225.            SUM_BDR + curr_font->head.wide*MIN_X +1,
  226.            SUM_BDR + curr_font->head.high*MIN_Y +1);
  227. #endif
  228.  
  229.    if (dx < SUM_BDR + curr_font->head.wide*MIN_X +1 ||
  230.        dy < SUM_BDR + curr_font->head.high*MIN_Y +1)
  231.        return(-1);
  232.  
  233. #ifdef DEBUG
  234.    dprintf(n)(stderr,"adjusted to: (%d,%d)  %d x %d\r\n",x,y,dx,dy);
  235. #endif
  236.  
  237.    if (!setup_window(win,curr_font,x,y,dx,dy)) {
  238.       fprintf(stderr,"Out of memory for window creation -- bye!\n");
  239.       quit();
  240.       }
  241.  
  242.    next_window++;
  243.  
  244.    /* make the window */
  245.  
  246.    set_covered(win);
  247.    border(win,BLK_BDR,WH_BDR);
  248.    CLEAR(W(window),BIT_CLR);
  249.  
  250.    /* set up file descriptor modes */
  251.  
  252.    if (fcntl(W(from_fd),F_SETFL,fcntl(W(from_fd),F_GETFL,0)|FNDELAY) == -1)
  253.       fprintf(stderr,"%s: fcntl failed for fd %d\n",W(tty),W(from_fd));
  254.  
  255.    mask |= 1<<W(to_fd);
  256.  
  257.    /* send initial string (if any) */
  258.  
  259.    if (start && *start) {
  260. #ifdef DEBUG
  261.       dprintf(n)(stderr,"Sending initial string: [%s]\n",start);
  262. #endif
  263.       Write(W(to_fd),start,strlen(start));
  264.       }
  265.    return(0);
  266.    }
  267.  
  268. /* initialize window state */
  269.  
  270. int
  271. setup_window(win,curr_font,x,y,dx,dy)
  272. register WINDOW *win;
  273. int x,y,dx,dy;
  274. struct font *curr_font;
  275.    {
  276.    register int i;
  277.  
  278. #ifdef ALIGN
  279.    alignwin(screen,&x,&dx,SUM_BDR);
  280. #endif
  281.  
  282.    W(font) = curr_font;
  283.    W(x) = 0;
  284.    W(y) = curr_font->head.high;
  285.    W(esc_cnt) = 0;
  286.    W(esc[0]) = 0;
  287.    W(flags) = W_ACTIVE | init_flags;
  288. #ifdef CUT
  289.    W(flags) |= W_SNARFABLE;
  290. #endif
  291. #ifdef COLOR
  292.    W(background) = NOCOLOR&BIT_SRC | GETCOLOR(WHITE);
  293.    W(style) = NOCOLOR&BIT_SRC | GETCOLOR(BLACK);
  294. #else
  295.    W(style) = OPCODE(BIT_SRC);
  296.    W(background) = OPCODE(BIT_CLR);
  297. #endif
  298.     W(curs_type) = CS_BLOCK;
  299.    W(x0) = x;
  300.    W(y0) = y;
  301.    W(border) = bit_create(screen,x,y,dx,dy);
  302.    W(window) = bit_create(W(border),SUM_BDR,SUM_BDR,dx-SUM_BDR*2,dy-SUM_BDR*2);
  303.  
  304.    W(text.x) = 0;
  305.    W(text.y) = 0;
  306.    W(text.wide) = 0;
  307.    W(text.high) = 0;
  308.  
  309.    W(bitmap) = (BITMAP *) 0;
  310.    for(i=0;i<MAXBITMAPS;i++)
  311.       W(bitmaps)[i] = (BITMAP *) 0;
  312.  
  313.    W(save) = (BITMAP *) 0;
  314.    W(stack) = (WINDOW *) 0;
  315.    W(main) = win;
  316.    W(alt) = (WINDOW *) 0;
  317.    W(esc_cnt) = 0;
  318.    W(esc[0])=0;
  319.    W(clip_list) = (char *) 0;
  320.  
  321.    for(i=0;i<MAXMENU;i++)
  322.       W(menus[i]) = (struct menu_state *) 0;
  323.  
  324.    W(menu[0]) = W(menu[1]) = -1;
  325.    W(event_mask) = 0;
  326.  
  327.    for(i=0;i<MAXEVENTS;i++)
  328.       W(events)[i] = (char *) 0;
  329.  
  330.    W(snarf) = (char *) 0;
  331.    W(gx) = 0;
  332.    W(gy) = 0;
  333.    W(op) = OPCODE(BIT_SET);
  334.    W(max) = 0;
  335.    W(current) = 0;
  336.    strcpy(W(tty), last_tty());
  337.    W(num) = 0;
  338.    clip_bad(win);    /* invalidate clip lists */
  339.    return(W(border) && W(window));
  340.    }
  341.  
  342. /*
  343.     Look through all the windows for the next available window set id.
  344. */
  345.  
  346. int
  347. next_windowset_id()
  348.    {
  349.       char        list[ MAXWIN + 2 ];
  350.       register char    *cp;
  351.       register WINDOW    *win;
  352.  
  353.       for( cp = list;  cp < &list[ MAXWIN + 2 ];  cp++ )
  354.      *cp = 0;
  355.  
  356.       for( win = active;  win != (WINDOW *)0;  win = W(next) )
  357.      list[ W(setid) ] = 1;
  358.  
  359.       /*    There is no window set ID zero.
  360.       */
  361.       for( cp = list + 1;  *cp;  cp++ )
  362.      ;
  363.  
  364.       return cp - list;
  365.    }
  366.